class Job {

    static searchable = false

//    Customer customer
    Date date
    BigDecimal discount = 1
    BigDecimal actualValue = 0
    String status = Constants.JOB_STATUS_CREATED
    Boolean pointAddToCustomer = false
    Integer printTimes = 0
    static hasMany = [jobItems: JobItem]
    static transients = ['value', 'originalValue', 'point', 'printable']
    static belongsTo = [customer: Customer]

    BigDecimal getValue() {
        return (getOriginalValue() * discount).setScale(2, BigDecimal.ROUND_HALF_UP)
    }

    BigDecimal getPoint() {
        return (actualValue.divide(0.7, 2, BigDecimal.ROUND_HALF_UP)).setScale(0, BigDecimal.ROUND_HALF_UP)
    }

    BigDecimal getOriginalValue() {
        BigDecimal value = 0;
        jobItems?.each {JobItem item ->
            value += item.value
        }
        return value
    }

    String toString() {
        return "交易 [单号：$id][日期：$date][金额：$actualValue]"
    }

    boolean getPrintable(){
        return printTimes == 0 && status == Constants.JOB_STATUS_PENDING
    }

}
